package com.helwen.utils;





import com.helwen.entity.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeDataUtil {


    /**
     * TODO 双层循环实现建树,对传入的树节点列表无排序要求
     * @param treeNodes 传入的树节点列表
     * @return
     */
    public static List<TreeNode> bulidByLoop(List<TreeNode> treeNodes) {

        List<TreeNode> trees = new ArrayList<TreeNode>();
        for (TreeNode treeNode : treeNodes) {
            /*
             * 根节点的父Id为0,且不要求仅有唯一的根
             */
            if ("0".equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (TreeNode it : treeNodes) {
                if (it.getParentId() == treeNode.getId()) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }

    /**
     * TODO 使用递归方法建树,对传入的树节点列表无排序要求
     * @param treeNodes
     * @return
     */
    public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {
        List<TreeNode> trees = new ArrayList<TreeNode>();
        for (TreeNode treeNode : treeNodes) {
            // 根节点的父Id为0,且不要求仅有唯一的根
            if ("0".equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    /**
     * TODO 递归查找子节点
     * @param treeNodes
     * @return
     */
    public static TreeNode findChildren(TreeNode treeNode,List<TreeNode> treeNodes) {
        for (TreeNode it : treeNodes) {
            if(treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<TreeNode>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }

    /**
     * TODO 通过Map缓存生成树
     * @param dataList 经过排序的集合,要求父节点必须位于集合第一个元素,且仅能存在唯一的根节点
     * @return tree.TreeNode
     **/
    public static TreeNode bulidByMapLink(List<TreeNode> dataList) {
        if (null == dataList || 0 == dataList.size()) {
            return null;
        }
        // 节点缓存
        Map<String, TreeNode> linkTree = new HashMap<String, TreeNode>();
        TreeNode temp = new TreeNode(dataList.get(0));
        // 缓存树结构
        linkTree.put(temp.getId(), temp);
        // 遍历数据除首条外的数据，建立节点关联关系
        for (int i = 1; i < dataList.size(); i++) {
            TreeNode node = new TreeNode(dataList.get(i));
            if (null != linkTree.get(dataList.get(i).getParentId())) {
                // 缓存中已存在相关父节点,子节点
                if (null == linkTree.get(dataList.get(i).getParentId()).getChildren()) {
                    // 缓存中子节点不存在
                    linkTree.get(dataList.get(i).getParentId()).setChildren(new ArrayList<TreeNode>());
                }
                linkTree.get(dataList.get(i).getParentId()).getChildren().add(node);
            }
            linkTree.put(node.getId(), node);
        }
        TreeNode reTree = linkTree.get(temp.getId());
        linkTree.clear();
        return reTree;
    }


}
